#include"MyPlayer.h"
#include<libavutil/parseutils.h>
AVFormatContext*fmt_ctx = NULL;
#if USE_FFMPEG_SWS
FF_VIDEOINFO videoinfo={-1,NULL,NULL,NULL,0};
#else
FF_VIDEOINFO videoinfo={ -1,NULL,NULL};
#endif
FF_AUDIOINFO audioinfo={-1,NULL,NULL,NULL,0};
void SeekTime(int times){
     int64_t us=0;
     char buff[32]={0};
     sprintf(buff,"%d",times);
     if(av_parse_time(&us, buff, 1)<0){
        printf("Get TimeStamp Failed\n");
     }else{
        if(fmt_ctx->start_time != AV_NOPTS_VALUE)
           us+=fmt_ctx->start_time;
        avformat_seek_file(fmt_ctx, -1, INT64_MIN, us, INT64_MAX, 0);
    }
    printf("Seek RealTimeStamp:%I64d\n",us);
}
int InitFFMpeg(const char*filename){
	int ret=0;
    av_register_all();
    avcodec_register_all();
	avformat_network_init();
    printf("Find Protocol:%s\n",avio_find_protocol_name(filename));
	if((ret = avformat_open_input(&fmt_ctx,filename, NULL, NULL))){
	    printf("OpenError:%d\n",ret);
        return ret;
	}    
    ShowMetaData(fmt_ctx->metadata);
    printf("Try Find StreamInfo\n");
    if(avformat_find_stream_info(fmt_ctx, NULL) < 0) {
        fprintf(stderr, "Could not find stream information\n");
        exit(1);
    }
    ShowAllStreams(fmt_ctx);
    ShowAllProgram(fmt_ctx);
    TryGetVideoCodec(&videoinfo,fmt_ctx);
#if USE_PLAY_AUDIO
    TryGetAudioCodec(&audioinfo,fmt_ctx);
#endif
	return 0;
}
void UnInitFFMpeg(){
	if(videoinfo.codec)avcodec_close(videoinfo.codec);
    if(audioinfo.codec)avcodec_close(audioinfo.codec);
    avformat_close_input(&fmt_ctx);
}
int FF_DEC_Thread(void*data){
//DTS Decoding Time Stamp
//PTS Presentation Time Stamp    
    AVFrame *pVideoFrame=av_frame_alloc();
    AVFrame *pAudioFrame=av_frame_alloc();
    AVPacket packet;
    printf("FF_DEC_Thread\n");
	//TestReadFrame();
    while(av_read_frame(fmt_ctx,&packet)>-1){
      if(packet.stream_index==videoinfo.idx){
         if(bVideoThreadRunning)TryDecVideoPacket(&videoinfo,&packet,pVideoFrame);
      }else
      if(packet.stream_index==audioinfo.idx){
		  if (bAudioThreadRunning) {
			  int ret = 0;
			  do {
				ret=TryDecAudioPacket(&audioinfo, &packet, pAudioFrame);
				if (ret < 0)break;
				packet.data += ret;
				packet.size -= ret;
			 }while (packet.size > 0);
		  }
      }
      av_free_packet(&packet);
      if(running==0)break;
	 
   }
   av_frame_free(&pVideoFrame);
   av_frame_free(&pAudioFrame);
   running=0;
   printf("FF_DEC_ThreadEnd\n");
   return 0;
}

void TestReadFrame() {
	int count = 0;
	AVPacket packet;
	AVFrame *frame = av_frame_alloc();
	int ret = 0;
	while (1) {
		if ((ret = av_read_frame(fmt_ctx, &packet)) < 0)
			break;

		if (packet.stream_index == videoinfo.idx) {
			//avcodec_get_frame_defaults(frame);
			int got_frame = 0;
			ret = avcodec_decode_video2(videoinfo.codec, frame, &got_frame, &packet);
			if (ret < 0) {
				av_log(NULL, AV_LOG_ERROR, "Error decoding video\n");
				break;
			}
			printf("got_frame:%d packet_pts:%d frame_pts:%d\n", got_frame, packet.pts, frame->pts);
		}
		av_free_packet(&packet);
		count++;
		if (count>100)
			break;
	}
	exit(0);
}
